import 'dart:async';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:system_info/system_info.dart';
import 'package:system_info2/system_info2.dart';

// 系统信息数据模型
class SystemInfoItem {
  final String key;
  final String value;

  SystemInfoItem(this.key, this.value);
}

void main() {
  // 收集系统信息
  List<SystemInfoItem> systemInfoList = [];
  
  // 内核信息
  String kernelArchitecture = SysInfo.kernelArchitecture.toString();
  systemInfoList.add(SystemInfoItem('Kernel architecture', kernelArchitecture));
  
  String rawKernelArchitecture = SysInfo.rawKernelArchitecture;
  systemInfoList.add(SystemInfoItem('Raw Kernel architecture', rawKernelArchitecture));
  
  String kernelBitness = SysInfo.kernelBitness.toString();
  systemInfoList.add(SystemInfoItem('Kernel bitness', kernelBitness));
  
  String kernelName = SysInfo.kernelName;
  systemInfoList.add(SystemInfoItem('Kernel name', kernelName));
  
  String kernelVersion = SysInfo.kernelVersion;
  systemInfoList.add(SystemInfoItem('Kernel version', kernelVersion));
  
  // 操作系统信息
  String osName = SysInfo.operatingSystemName;
  systemInfoList.add(SystemInfoItem('Operating system name', osName));
  
  String osVersion = SysInfo.operatingSystemVersion;
  systemInfoList.add(SystemInfoItem('Operating system version', osVersion));
  
  // 用户信息
  String userDir = SysInfo.userDirectory;
  systemInfoList.add(SystemInfoItem('User directory', userDir));
  
  String userId = SysInfo.userId.toString();
  systemInfoList.add(SystemInfoItem('User id', userId));
  
  String userName = SysInfo.userName;
  systemInfoList.add(SystemInfoItem('User name', userName));
  
  String userSpaceBitness = SysInfo.userSpaceBitness.toString();
  systemInfoList.add(SystemInfoItem('User space bitness', userSpaceBitness));

  print('Kernel architecture     : $kernelArchitecture');
  print('Raw Kernel architecture : $rawKernelArchitecture');
  print('Kernel bitness          : $kernelBitness');
  print('Kernel name             : $kernelName');
  print('Kernel version          : $kernelVersion');
  print('Operating system name   : $osName');
  print('Operating system version: $osVersion');
  print('User directory          : $userDir');
  print('User id                 : $userId');
  print('User name               : $userName');
  print('User space bitness      : $userSpaceBitness');
  // CPU核心信息
  final cores = SysInfo.cores;
  systemInfoList.add(SystemInfoItem('Number of core', cores.length.toString()));
  print('Number of core    : ${cores.length}');
  
  for (int i = 0; i < cores.length; i++) {
    final core = cores[i];
    systemInfoList.add(SystemInfoItem('Core $i - Architecture', core.architecture.toString()));
    systemInfoList.add(SystemInfoItem('Core $i - Name', core.name));
    systemInfoList.add(SystemInfoItem('Core $i - Socket', core.socket.toString()));
    systemInfoList.add(SystemInfoItem('Core $i - Vendor', core.vendor));
    
    print('Architecture          : ${core.architecture}');
    print('Name                  : ${core.name}');
    print('Socket                : ${core.socket}');
    print('Vendor                : ${core.vendor}');
  }
  
  // 内存信息
  const int megaByte = 1024 * 1024;
  String totalPhysicalMemory = '${SysInfo.getTotalPhysicalMemory() ~/ megaByte} MB';
  systemInfoList.add(SystemInfoItem('Total physical memory', totalPhysicalMemory));
  
  String freePhysicalMemory = '${SysInfo.getFreePhysicalMemory() ~/ megaByte} MB';
  systemInfoList.add(SystemInfoItem('Free physical memory', freePhysicalMemory));
  
  String totalVirtualMemory = '${SysInfo.getTotalVirtualMemory() ~/ megaByte} MB';
  systemInfoList.add(SystemInfoItem('Total virtual memory', totalVirtualMemory));
  
  String freeVirtualMemory = '${SysInfo.getFreeVirtualMemory() ~/ megaByte} MB';
  systemInfoList.add(SystemInfoItem('Free virtual memory', freeVirtualMemory));

  String virtualMemorySize = '${SysInfo.getVirtualMemorySize() ~/ megaByte} MB';
  systemInfoList.add(SystemInfoItem('Virtual memory size', virtualMemorySize));

  print('Total physical memory   '': $totalPhysicalMemory');
  print('Free physical memory    '': $freePhysicalMemory');
  print('Total virtual memory    '': $totalVirtualMemory');
  print('Free virtual memory     '': $freeVirtualMemory');
  print('Virtual memory size     '': $virtualMemorySize');
  // 运行应用
  runApp(MyApp(systemInfoList: systemInfoList));
}

class MyApp extends StatelessWidget {
  final List<SystemInfoItem> systemInfoList;

  const MyApp({super.key, required this.systemInfoList});

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: const Text('系统信息'),
        ),
        body: ListView.builder(
          itemCount: systemInfoList.length,
          itemBuilder: (context, index) {
            final item = systemInfoList[index];
            return Container(
              padding: const EdgeInsets.all(12.0),
              decoration: BoxDecoration(
                border: Border(bottom: BorderSide(color: Colors.grey.shade200))
              ),
              child: Column(
                crossAxisAlignment: CrossAxisAlignment.start,
                children: [
                  Text(
                    '${item.key}:',
                    style: TextStyle(
                      fontWeight: FontWeight.bold,
                      color: Colors.blue.shade700
                    ),
                  ),
                  const SizedBox(height: 4),
                  Text("  ${item.value}"),
                ],
              ),
            );
          },
        ),
      ),
    );
  }
}
